package com.jcfk.system.utils;

import com.jcfk.common.core.exception.CustomException;

import java.util.HashMap;
import java.util.Map;

public class CheckSeqUtils {

    public static void checkSeq(String plainText) {
        String regex = "[一二三四五六七八九十零佰百仟千万0-9.-]+";
        boolean isMatch = plainText.matches(regex);

        if (!isMatch) {
            throw new CustomException("序号格式只能为'一','1.1','1-1'这类格式");
        }
    }


    private static final Map<Character, Integer> CHINESE_NUMBER_MAP = createChineseNumberMap();

    private static Map<Character, Integer> createChineseNumberMap() {
        Map<Character, Integer> map = new HashMap<>();
        map.put('零', 0);
        map.put('一', 1);
        map.put('二', 2);
        map.put('三', 3);
        map.put('四', 4);
        map.put('五', 5);
        map.put('六', 6);
        map.put('七', 7);
        map.put('八', 8);
        map.put('九', 9);
        return map;
    }

    public static int convertChineseNumberToArabic(String chineseNumber) {
        int result = 0;
        int temp = 0;
        for (char c : chineseNumber.toCharArray()) {
            if (CHINESE_NUMBER_MAP.containsKey(c)) {
                int digit = CHINESE_NUMBER_MAP.get(c);
                if (digit >= 10) {
                    result += temp;
                    result *= digit;
                    temp = 0;
                } else {
                    temp = temp * 10 + digit;
                }
            }
        }
        result += temp;
        return result;
    }
}
